package top.shiyiri.dao.junit;

import org.junit.Test;
import top.shiyiri.bean.Customer;
import top.shiyiri.dao.impl.CustomerDaoImpl;
import top.shiyiri.util.JDBCUtils;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

/**
 * @author Aunean
 * @date 2022/1/19 21:56
 */
public class CustomerDaoImplTest {
    private CustomerDaoImpl dao = new CustomerDaoImpl();

    @Test
    public void insertCustomer() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            Customer customer = new Customer(1, "弊端", "23423@163.com", new Date(2479324294L));
            dao.insertCustomer(conn, customer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }

    @Test
    public void deleteById() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            dao.deleteById(conn, 4);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }

    @Test
    public void updateById() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            Customer customer = new Customer(5, "牛逼", "23423@163.com", new Date(22479324294L));
            dao.updateById(conn, customer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }

    @Test
    public void getCustomerById() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection2();
            Customer customerById = dao.getCustomerById(conn, 20);
            System.out.println(customerById);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }

    @Test
    public void getAllCustomerList() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getDataSource();
            List<Customer> allCustomerList = dao.getAllCustomerList(conn);
            allCustomerList.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }

    @Test
    public void getCount() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection3();
            Long lines = dao.getCount(conn);
            System.out.println(lines);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }

    @Test
    public void getMaxBirth() {
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            java.sql.Date maxBirth = dao.getMaxBirth(conn);
            System.out.println(maxBirth);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }
}